@echo off
setlocal enabledelayedexpansion

:: 设置编译器和构建类型
set BUILD_TYPE=Release
set GENERATOR="Visual Studio 17 2022"
set BUILD_DIR=build

echo ===== DRC System Build Script =====

:: 默认执行清理，除非指定 noclean
if not "%1"=="noclean" (
    echo Cleaning build directory...
    if exist %BUILD_DIR% rmdir /s /q %BUILD_DIR%
)

:: 创建构建目录
if not exist %BUILD_DIR% mkdir %BUILD_DIR%

:: 进入构建目录
cd %BUILD_DIR%

echo Configuring CMake...
cmake .. -G %GENERATOR% ^
    -DCMAKE_BUILD_TYPE=%BUILD_TYPE% ^
    -DPYTHON_EXECUTABLE=python

if %ERRORLEVEL% neq 0 (
    echo CMake configuration failed!
    exit /b 1
)

echo Building project...
cmake --build . --config %BUILD_TYPE%

if %ERRORLEVEL% neq 0 (
    echo Build failed!
    exit /b 1
)

echo Build completed successfully!

:: 返回原目录
cd ..

:: 运行deploy.py
echo.
echo Running deployment script...
python deploy.py
if %ERRORLEVEL% neq 0 (
    echo Deployment failed!
    pause
    exit /b 1
)

:: 如果构建成功，询问是否运行示例程序
echo.
echo Do you want to run the example program? [Y/N]
set /p RUN_EXAMPLE=

if /i "%RUN_EXAMPLE%"=="Y" (
    echo Running example program...
    cd deploy
    call run.bat
    cd ..
)

echo.
echo ===== Build Process Completed =====
pause 